package com.wealth.system.web.controller;

import iaas.redis.RedisService;
import iaas.utils.ResultObject;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.member.entity.Member;
import com.wealth.member.service.IMemberService;


@Controller
@RequestMapping(value="/backPwd")
public class BackPwdController extends BaseController {

	Logger logger = Logger.getLogger(BackPwdController.class);
	
	@Autowired
	IMemberService memberService;
	
	
	@Autowired
	private RedisService redisService;
	 
	/**
	 * 找回密码页面
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String findPwd(){
		return "setting/backPwd";
	}
	
	/**
	 * 找回密码
	 * FIXME 限制次数
	 */
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	public ResultObject doReg(Member member,String random,RedirectAttributes redirectAttributes,HttpServletRequest request){
		try {
			/** -------- redis获取验证码 -------- **/
			String randomInRedis = redisService.get("redis_sms_"+member.getUsername());
			if(!randomInRedis.equals(random)){
				return new ResultObject("500", "验证码错误", null);
			}
			
			/** -------- 检查用户名-------- **/
			Member temp = memberService.findMemberByUsername(member.getUsername());
			if(temp==null){
				return new ResultObject("500", "会员名不存在", null);
			}
			
			/** -------- 修改会员密码-------- **/
			temp.setPassword(member.getPassword());
			memberService.updatePassword(temp);
			
			return new ResultObject("200", "密码修改成功", null);
		} catch (ServiceException e) {
			logger.error("", e);
			return new ResultObject("500", "注册出错", null);
		}
	}
}
